package com.example.cake.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cake.common.Result;
import com.example.cake.entity.DTO.SysCakeDTO;
import com.example.cake.entity.DTO.SysClassifyDTO;
import com.example.cake.entity.SysCake;
import com.example.cake.entity.SysClassify;
import com.example.cake.entity.SysCrowd;
import com.example.cake.entity.SysFlavor;
import com.example.cake.service.SysCakeService;
import com.example.cake.service.SysClassifyService;
import com.example.cake.service.SysCrowdService;
import com.example.cake.service.SysFlavorService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/cake")
public class SysCakeController {
    @Autowired
    private SysCakeService sysCakeService;
    @Autowired
    private SysClassifyService sysClassifyService;
    @Autowired
    private SysFlavorService sysFlavorService;
    @Autowired
    private SysCrowdService sysCrowdService;
    @PostMapping("/list")
    public Result list(@RequestBody SysCakeDTO sysCakeDTO) {
        LambdaQueryWrapper<SysCake> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(sysCakeDTO.getName()), SysCake::getName,sysCakeDTO.getName());
        lqw.like(Strings.isNotEmpty(sysCakeDTO.getSize()), SysCake::getSize,sysCakeDTO.getSize());
        lqw.like(sysCakeDTO.getClassifyId()!= null, SysCake::getClassifyId,sysCakeDTO.getClassifyId());
        lqw.like(sysCakeDTO.getFlavorId()!= null, SysCake::getFlavorId,sysCakeDTO.getFlavorId());
        lqw.like(sysCakeDTO.getCrowdId()!= null, SysCake::getCrowdId,sysCakeDTO.getCrowdId());
        lqw.orderByDesc(SysCake::getUpdateTime);
        Page<SysCake> page = new Page<>(sysCakeDTO.getPage(), sysCakeDTO.getLimit());
        Page<SysCake> cakePage = sysCakeService.page(page, lqw);
        cakePage.getRecords().forEach(item->{
            SysClassify classify = sysClassifyService.getById(item.getClassifyId());
            item.setClassifyName(classify.getName());
            SysFlavor sysFlavor = sysFlavorService.getById(item.getFlavorId());
            item.setClassifyName(classify.getName());
            SysCrowd sysCrowd = sysCrowdService.getById(item.getCrowdId());
            item.setClassifyName(classify.getName());
            item.setCrowdName(sysCrowd.getName());
            item.setFlavorName(sysFlavor.getName());
        });
        return Result.success(cakePage);
    }
    @PostMapping("/getCakelist")
    public Result getCakelist(@RequestBody SysCakeDTO sysCakeDTO) {
        LambdaQueryWrapper<SysCake> lqw = new LambdaQueryWrapper<>();
        lqw.like(sysCakeDTO.getClassifyId()!= null, SysCake::getClassifyId,sysCakeDTO.getClassifyId());
        lqw.like(sysCakeDTO.getFlavorId()!= null, SysCake::getFlavorId,sysCakeDTO.getFlavorId());
        lqw.like(sysCakeDTO.getCrowdId()!= null, SysCake::getCrowdId,sysCakeDTO.getCrowdId());
        lqw.orderByDesc(SysCake::getUpdateTime);
        Page<SysCake> page = new Page<>(sysCakeDTO.getPage(), sysCakeDTO.getLimit());
        Page<SysCake> cakePage = sysCakeService.page(page, lqw);
        return Result.success(cakePage);
    }

    @PostMapping("/add")
    public Result add(@RequestBody SysCake sysCake) {
        sysCake.setCreateTime(DateUtil.toLocalDateTime(new Date()));
        sysCake.setUpdateTime(DateUtil.toLocalDateTime(new Date()));
        return Result.success(sysCakeService.save(sysCake));
    }
    @PostMapping("/getOne")
    public Result getOne(@RequestBody Integer id) {
        return Result.success(sysCakeService.getById(id));
    }

    @PostMapping("/remove")
    public Result add(@RequestBody Integer id) {
        return Result.success(sysCakeService.removeById(id));
    }

    @PostMapping("/update")
    public Result update(@RequestBody SysCake sysCake) {
        sysCake.setUpdateTime(DateUtil.toLocalDateTime(new Date()));
        return Result.success(sysCakeService.updateById(sysCake));
    }
}
